#!/usr/bin/env python
import StringIO as S
import jscrunch as Cr
import urllib as U
from pyquery import PyQuery as pq
import sys
import re
if len(sys.argv)<3:
    print "USAGE: %s [jsfile] [htmlfile]" % sys.argv[0]
    exit(-1)

with open(sys.argv[1]) as js:
    with open(sys.argv[2],'r+') as htm:    
        sys.stdin = js
        bmout = S.StringIO()
        sout,sys.stdout = sys.stdout, bmout
        Cr.crunch()
        esc = re.compile("""[ %"|\\^]""")
        bm = "javascript:(function(){%s})()" % esc.sub(lambda m:U.quote(m.group(0)),bmout.getvalue())
        print >>sout, bm
##        h = pq(htm.read())
##        h('body').append(pq('<a id="bmTest" href="%s">bookmarklet test</a>')%bm)
##        htm.seek(0)
##        htm.write(h.html())

        
